package code.tools.commponents
{
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.font.FontManager;
	import code.structure.module.config.data.ShopItemData;
	import code.untils.FiltersEnum;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	/**
	 *icon + 'x' + 数量 
	 * 100 * 100
	 * @author liangqian
	 * 
	 */	
	public class IconItem extends Component
	{
		private var icon:*;
		
		private var _numTf:TextField;
		private var _numSize:int;
		private var _numColor:uint;
		public function IconItem(parent:DisplayObjectContainer, xpos:Number, ypos:Number,numColor:uint = 0x579514,numSize:int = 15)
		{
			_numColor = numColor;
			_numSize = numSize;
			super(parent, xpos, ypos);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			
			graphics.beginFill(0,0);
//			graphics.lineStyle(1,0);
			graphics.drawRect(0,0,width,height);
			graphics.endFill();
			_numTf = Helpers.FormatTextField(this,0,70,85,TextFieldAutoSize.RIGHT,_numColor,FontManager.getInstance().embeddedFontBName,_numSize);
			_numTf.mouseEnabled = false;
			_numTf.filters = [FiltersEnum.NUM_GLOW_FILTER];
		}
		
		override protected function init():void
		{
			setSize(100,100);
			super.init();
		}
		
		private var _d:ShopItemData;
		private var _isMc:Boolean;
		public function updateIconAndNum(shopItemData:ShopItemData,num:int = 1,isMc:Boolean = false):void
		{
			_d = shopItemData;
			_isMc = isMc;
			if(shopItemData == null)
			{
				clearIcon();
				visible = false;
				return;
			}
			_numTf.text = 'x' + num;
			DllLoaderManager.getInstance().getMaterial(shopItemData.sPath,'ICON',loadCompleteHandler,shopItemData.fileType);
			visible = true;
		}
		
		public function get data():ShopItemData
		{
			return _d;
		}
		
		private function loadCompleteHandler(c:*, key:String = ''):void
		{
			clearIcon();
			if(_d == null)return;
			if(key != _d.sPath + '&' && key != _d.sPath + '&ICON')return;
			if(_isMc)
				icon = Helpers.drawMc(c,icon);
			else
				icon = Helpers.drawBmd(c,icon);
			addChild(icon);
			addChild(_numTf);
		}
		
		private function clearIcon():void
		{
			if(icon && this.contains(icon))
			{
				removeChild(icon);
				icon = null;
			}
		}
	}
}